package Handler;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DAO.*;
import Modelo.*;



@WebServlet("/VehiculoHandler")
public class VehiculoHandler extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	private static String ADD = "/VehiculoAdd.jsp";
    private static String Edit = "/Vehiculoedit.jsp";
    private static String vehiculosRecord="/ListVehiculos.jsp";
    private VehiculoDao dao;
    
    public VehiculoHandler() {
        super();
        dao = new VehiculoDao();
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String redirect="";
        String vehId = request.getParameter("vehiculoid");        
        String action = request.getParameter("action");
        
		if (action.equalsIgnoreCase("editform")){         
		        redirect = Edit;            
		}
		else if (action.equalsIgnoreCase("addnew")){         
		        redirect = ADD;            
		}else if (action.equalsIgnoreCase("listCategorias")) {
			redirect = vehiculosRecord;
			request.setAttribute("categorias", dao.getAllVehiculos());
		} 
		
		 RequestDispatcher rd = request.getRequestDispatcher(redirect);
		 rd.forward(request, response);
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String redirect="";
        String vehId = request.getParameter("vehiculoid");        
        String action = request.getParameter("action");
        
		if (action.equalsIgnoreCase("insert")) {
			Vehiculo objvehiculo = new Vehiculo();
			objvehiculo.setPlaca(request.getParameter("Placa"));
			objvehiculo.setMarca(request.getParameter("Marca"));
			objvehiculo.setModelo(Integer.parseInt(request.getParameter("Modelo")));
			objvehiculo.setCategoriaId(Integer.parseInt(request.getParameter("Categoria")));
			objvehiculo.setPrototipo(request.getParameter("Linea"));
			objvehiculo.setCapacidad_Max(Integer.parseInt(request.getParameter("Cap_Max")));
			objvehiculo.setPeso_Max(Float.parseFloat(request.getParameter("P_Max")));
			objvehiculo.setDimensiones_Carga(request.getParameter("Dim_Carga"));
			objvehiculo.setEstado(Integer.parseInt(request.getParameter("Estado_V")));
			dao.addVehiculo(objvehiculo);
			redirect = vehiculosRecord;
			request.setAttribute("Categorias", dao.getAllVehiculos());
			System.out.println("Record Added Successfully");
		}

		else if (action.equalsIgnoreCase("delete")) {
			String veh_Id = request.getParameter("VehiculoId");
			int vehid = Integer.parseInt(veh_Id);
			dao.removeVehiculo(vehid);
			redirect = vehiculosRecord;
			request.setAttribute("categorias", dao.getAllVehiculos());
			System.out.println("Record Deleted Successfully");

		} else if (action.equalsIgnoreCase("edit")) {
			String veh_Id = request.getParameter("VehiculoId");
			int vehid = Integer.parseInt(veh_Id);
			Vehiculo objvehiculo = new Vehiculo();
			objvehiculo.setVehiculoId(vehid);
			objvehiculo.setPlaca(request.getParameter("Placa"));
			objvehiculo.setMarca(request.getParameter("Marca"));
			objvehiculo.setModelo(Integer.parseInt(request.getParameter("Modelo")));
			objvehiculo.setCategoriaId(Integer.parseInt(request.getParameter("Categoria")));
			objvehiculo.setPrototipo(request.getParameter("Linea"));
			objvehiculo.setCapacidad_Max(Integer.parseInt(request.getParameter("Cap_Max")));
			objvehiculo.setPeso_Max(Float.parseFloat(request.getParameter("P_Max")));
			objvehiculo.setDimensiones_Carga(request.getParameter("Dim_Carga"));
			objvehiculo.setEstado(Integer.parseInt(request.getParameter("Estado_V")));
			dao.editVehiculo(objvehiculo);
			request.setAttribute("categoria", objvehiculo);
			redirect = vehiculosRecord;
			System.out.println("Record updated Successfully");

		} else if (action.equalsIgnoreCase("listCategorias")) {
			redirect = vehiculosRecord;
			request.setAttribute("categorias", dao.getAllVehiculos());
		} else {
			redirect = ADD;
		}

    RequestDispatcher rd = request.getRequestDispatcher(redirect);
    rd.forward(request, response);
		
	}

}
